package com.tang.service.logic;


import com.tang.constance.LimitConstance;
import com.tang.entity.CarInfo;
import com.tang.model.vo.TreeNodeLink;

import java.util.List;

/**
 * 逻辑决策公共方法抽象类
 *
 * @author fulin.tang
 */
public abstract class BaseLogic implements LogicFilter {

    @Override
    public Long filter(Object matterValue, List<TreeNodeLink> treeNodeLinkList) {
        // 循环所有节点
        for (TreeNodeLink nodeLine : treeNodeLinkList) {
            // 返回符合条件的
            if (decisionLogic(matterValue, nodeLine)) {
                return nodeLine.getNodeIdTo();
            }
        }
        return 0L;
    }

    @Override
    public abstract Object matterValue(Long treeId, String userId, CarInfo carInfo);

    /**
     * 判断是否满足条件
     *
     * @param matterValue 比较标识
     * @param nodeLink 条件分支
     * @return 该分支是否满足条件
     */
    private boolean decisionLogic(Object matterValue, TreeNodeLink nodeLink) {

        switch (nodeLink.getRuleLimitType()) {
            case LimitConstance.EQUALS:
                return matterValue.equals(nodeLink.getRuleLimitValue());
            case LimitConstance.GREATER:
                return Double.parseDouble(String.valueOf(matterValue)) > Double.parseDouble(String.valueOf(nodeLink.getRuleLimitValue()));
            case LimitConstance.LESS:
                return Double.parseDouble(String.valueOf(matterValue)) < Double.parseDouble(String.valueOf(nodeLink.getRuleLimitValue()));
            case LimitConstance.LESS_OR_EQUALS:
                return Double.parseDouble(String.valueOf(matterValue)) <= Double.parseDouble(String.valueOf(nodeLink.getRuleLimitValue()));
            case LimitConstance.GREATER_OR_EQUALS:
                return Double.parseDouble(String.valueOf(matterValue)) >= Double.parseDouble(String.valueOf(nodeLink.getRuleLimitValue()));
            default:
                return false;
        }
    }

}
